package com.open.server.api.system;


import com.open.common.annotation.MethodPermission;
import com.open.base.model.Menu;
import com.open.system.dispatch.IMenuDispatch;
import com.open.common.model.Result;
import com.open.system.model.dto.MenuDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 *
 * @author 大树03
 * @date 2025-02-01 22:57
 */
@RestController
@RequestMapping("/system/menu")
public class MenuApi {

    @Autowired
    private IMenuDispatch menuDispatch;

    /**
     * 对象添加
     *
     * @param model 添加的对象
     * @return
     */
    @PostMapping("save")
    @MethodPermission("menu_add")
    public Result<Menu> save(@RequestBody Menu model) {
        long now = System.currentTimeMillis();
        Result<Menu> result = new Result<>();
        menuDispatch.save(model);
        result.setData(model);
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 对象修改
     *
     * @param model 修改的对象
     * @return
     */
    @PostMapping("update")
    @MethodPermission("menu_update")
    public Result<Boolean> update(@RequestBody Menu model) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(menuDispatch.update(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据对象列表查询
     *
     * @param model 查询条件
     * @return
     */
    @PostMapping("findByModel")
    public Result<List<Menu>> findByModel(@RequestBody Menu model) {
        long now = System.currentTimeMillis();
        Result<List<Menu>> result = new Result<>();
        result.setData(menuDispatch.findByModel(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据主键查询对象
     *
     * @param id 查询的对象主键
     * @return
     */
    @GetMapping("findByPk")
    public Result<Menu> findByPk(@RequestParam("id") Long id) {
        long now = System.currentTimeMillis();
        Result<Menu> result = new Result<>();
        result.setData(menuDispatch.findByPk(id));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 树形结构查询
     *
     * @param menu 树形结构查询条件
     * @return
     */
    @PostMapping("findTree")
    @MethodPermission("menu_query")
    public Result<List<Menu>> findTree(@RequestBody(required = false) Menu menu) {
        long now = System.currentTimeMillis();
        Result<List<Menu>> result = new Result<>();
        result.setData(menuDispatch.findTree(menu));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据主键删除对象
     *
     * @param ids 删除的对象主键
     * @return
     */
    @GetMapping("remove")
    @MethodPermission("menu_delete")
    public Result<Boolean> remove(@RequestParam("ids") Long ids) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(menuDispatch.remove(ids));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 根据角色id查询菜单
     *
     * @param roleId
     * @return
     */
    @GetMapping("/getByRoleId/{roleId}")
    public Result<List<Menu>> getByRoleId(@PathVariable Long roleId) {
        long now = System.currentTimeMillis();
        Result<List<Menu>> result = new Result<>();
        result.setData(menuDispatch.getByRoleId(roleId));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 分配权限
     * @param menuDto
     * @return
     */
    @PostMapping("/doAssign")
    @MethodPermission("menu_update")
    public Result<Boolean> doAssign(@RequestBody MenuDto menuDto) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(menuDispatch.doAssign(menuDto));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }
}